package com.syxp.pda.action.base;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import com.google.gson.JsonObject;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.User;
import com.syxp.pda.entity.system.Role;
import com.syxp.pda.service.base.UserService;

public class LoginAction extends BaseAction {
	@Resource
	private UserService userService;
	
	private String userAccount;
	private String userPwd;
	
	public String login() {
		try{
			System.out.println(userPwd+"----"+userAccount);
			User user = this.userService.getUserByAccount(this.userAccount);
			if(user.getPassWord().equals(this.userPwd)) {

//				JsonObject jobj = new JsonObject() ;
//				jobj.addProperty("userid", String.valueOf(user.getUserID())) ;
//				jobj.addProperty("useraccount", user.getUserAccount()) ;
//				jobj.addProperty("username", user.getUserName()) ;
				Role role = user.getRoles().size()<1?null:(Role)(user.getRoles().toArray()[0]);
//				jobj.addProperty("userrole", role==null?"":role.getRoleName()) ;
//				jobj.addProperty("department", user.getDepartment().getDepName()) ;
//				jobj.addProperty("success", true) ;
//				this.jsonString=super.gson.toJson(jobj) ;
				session.put("userid", user.getUserID());
				session.put("useraccount", user.getUserAccount());
				session.put("username", user.getUserName());
				session.put("userrole", role==null?"":role.getRoleName());
				session.put("department", user.getDepartment().getDepName());
				session.put("roleid", role==null?-1:role.getRoleID());
				this.jsonString="{success:true}" ;
			} else {
				this.jsonString="{success:false}" ;
			}
			
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
	
	public String logout() {
		try{
			this.session.clear();
			((HttpSession)this.session).invalidate();
			this.jsonString="{success:true}" ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
	
	/**
	 * @return the userService
	 */
	public UserService getUserService() {
		return userService;
	}
	/**
	 * @param userService the userService to set
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	/**
	 * @return the userAccount
	 */
	public String getUserAccount() {
		return userAccount;
	}
	/**
	 * @param userAccount the userAccount to set
	 */
	public void setUserAccount(String userAccount) {
		this.userAccount = userAccount;
	}
	/**
	 * @return the userPwd
	 */
	public String getUserPwd() {
		return userPwd;
	}
	/**
	 * @param userPwd the userPwd to set
	 */
	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}
	
}
